package com.project8.book;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import android.os.Environment;


public class StyleChanger 
{
	//This function saves the content of the notebook on a file (Overwrites previous contents).
	public static void writeContents(int mode, String type, String size, String font, String src) 
	{
		//Add new css tags like P{} h{} etc here -->
		String otherCSS = 	"img.alignLeft { float:left; margin-right: 2em; margin-top: 2em; margin-bottom: 1em;}   \n " +
							"img.alignRight { float:right; margin-left: 2em; margin-top: 2em; margin-bottom: 1em;}" +
							"td {padding: 5px;}" +
							"td.right {text-align: right; padding:5px;}" +
							"td.center {text-align: center; padding:5px;}" +
							"td.text {padding:10px;}" +
							"img {border: none;}" +
							"label {display:block;}\n" +
							".quiz {width:90%; margin:auto;}\n" +
							".quiz form {padding:3px; border:0px solid #7F8778; border-left-width:0px; border-bottom-width:5px; border-right-width:6px; background-color:#40664D;}\n" +
							".quiz p {font: 120% arial; padding:3px;border:2px solid #40664D; background-color:#C0D9AF;}\n" +
							".quiz p b {font:190% verdana;color:#006666;}\n" +
							".question {font:160% verdana;color:#FFFAF0;margin-bottom:15px; background-color:#40664D;}\n" +
							".answers {font:145% arial; color:#000033; background-color:#C0D9AF; padding:3px;}";
		
		String exStorageState = Environment.getExternalStorageState();
		Writer writer = null;
		
		if (Environment.MEDIA_MOUNTED.equals(exStorageState))
		{
			try 
			{//Write the file
				String table = "";				
				String source = "";
				String body = "";
				String pstyle = "";	
				String h1 = "h1 {font-family: \"Arial\"; src: url(\"../assets/fonts/arial.ttf\"); font-size: 200%;}" + "\n" +
							"h1.center {font-family: \"Arial\"; src: url(\"../assets/fonts/ARIAL.ttf\"); font-size: 200%; text-align:center;}" + "\n" +
							"h1.glossary {font-family: \"Arial\"; src: url(\"../assets/fonts/ARIAL.ttf\"); font-size: 150%; text-align:center;}";
				
				String h2 = "h2.section {font-family: \"Arial\"; src: url(\"../assets/fonts/ARIAL.ttf\"); font-size:150%; color:#00688b;}" + "\n" +
							"h4.section {font-family: \"Arial\"; src: url(\"../assets/fonts/ARIAL.ttf\"); font-size:150%; color:#00688b;}";

				String links = 	"a.out:link {color:#53868B;}" + "\n" +
								"a.out:visited {color:#53868B;}" + "\n" +
								"a.out:hover {color:#7AC5CD; text-decoration:none;}" + "\n" +
								"a.out:active {color:#CC1100; text-decoration:none;}";		
				
				String glossaryLinks = 	"a.glossary:link {color:#53868B; text-decoration:none;}" + "\n" +
										"a.glossary:visited {color:#53868B; text-decoration:none;}" + "\n" +
										"a.glossary:hover {color:#7AC5CD; text-decoration:none;}" + "\n" +
										"a.glossary:active {color:#CC1100; text-decoration:none;}";
				
				String bookLinks = 	"a.page:link {color:#EE4000; text-decoration:none;}" + "\n" +
				"a.page:visited {color:#EE4000; text-decoration:none;}" + "\n" +
				"a.page:hover {color:#8A2BE2; text-decoration:none;}" + "\n" +
				"a.page:active {color:#8A2BE2; text-decoration:none;}";
				
				int smallfont = (Integer.parseInt(size.trim())) - 3;
				
				if(mode == 1)
				{
						body = "body {background-color:#000000;" + "font-family: \"arial\"; src: url(\"../assets/fonts/arial.ttf\");" + "font-size: " + size.trim() + "px;}";
											
						pstyle = 	"p {font-size: " + size.trim() + "px; color:#FFFFFF;}" +
									"p.code {font-family: \"courier new\"; src: url(\"../assets/fonts/cour.ttf\");" + "font-size: " + size.trim() + "px; color:#FFFFFF;}" +
									"p.caption {font-size: " + smallfont +"px;}" +
									"div.code {font-family: \"courier new\"; src: url(\"../assets/fonts/cour.ttf\");" + "font-size: " + size.trim() + "px; color:#FFFFFF;}" +
									"sup {font-family: \"courier new\"; src: url(\"../assets/fonts/cour.ttf\");" + "font-size: " + (smallfont - 3) + "px; color:#EE0000;}";
									
						table = 	"table.main {width: 95%; margin:auto; border:0px solid #FFFFFF; background-color:#FAEBD7;}" +
									"td.header {border:0px solid #FFFFFF; border-bottom-width:2px; border-top-width:2px; text-align: right; padding:5px;}" +
									"td.bottomBorder {border:0px solid #FFFFFF; border-bottom-width:2px;}" +
									"td.shadow {border:0px solid #FFFFFF; border-bottom-width:5px; border-right-width:5px;}" +
									"table.docutils {border:1px solid #FFFFFF;}" +
									"td.border {border:1px solid #FFFFFF;}";

				}
				else
				{
						body = "body {background-color:#FFFFFF;" + "font-family: \"arial\"; src: url(\"../assets/fonts/arial.ttf\");" + "font-size: " + size.trim() + "px;}";

						pstyle = 	"p {font-size: " + size.trim() + "px; color:#FFFFFF;}" +
									"p.code {font-family: \"courier new\"; src: url(\"../assets/fonts/cour.ttf\");" + "font-size: " + size.trim() + "px; color:#000000;}" +
									"p.caption {font-size: " + smallfont +"px;}" +
									"div.code {font-family: \"courier new\"; src: url(\"../assets/fonts/cour.ttf\");" + "font-size: " + size.trim() + "px; color:#000000;}" +
									"sup {font-family: \"courier new\"; src: url(\"../assets/fonts/cour.ttf\");" + "font-size: " + (smallfont - 3) + "px; color:#EE0000;}";
						
						table = 	"table.main {width: 95%; margin:auto; border:0px solid #000000; background-color:#FAEBD7;}" +
									"td.header {border:0px solid #000000; border-bottom-width:2px; border-top-width:2px; text-align: right; padding:5px;}" +
									"td.bottomBorder {border:0px solid #000000; border-bottom-width:2px;}" +
									"td.shadow {border:0px solid #000000; border-bottom-width:5px; border-right-width:5px; background-color:#DEDEDE;}" +
									"table.docutils {border:1px solid #000000;}" +
									"td.border {border:1px solid #000000;}";
				}
				
			
				
				//Additional css is concatenated 
				source = source + body + "\n\n" + h1 + "\n\n" + h2 + "\n\n" + pstyle + "\n\n" + links + "\n\n" + glossaryLinks + "\n\n" + bookLinks + "\n\n" + otherCSS;
				
				System.out.println(source);
				
				File root = Environment.getExternalStorageDirectory();
				String path = root+"/css/";
				
				boolean exists = (new File(path).exists());
				
				if (!exists) {new File(path).mkdirs();}
					
				File file = new File(path+"Styles.css");
				file.createNewFile();
				
				System.out.println(path+"Styles.css");
				
				writer = new BufferedWriter(new FileWriter(file));
				writer.write(source);
			} 
			catch (FileNotFoundException e) 
			{
				e.printStackTrace();	
			} 
			catch (IOException e) 
			{
				e.printStackTrace();
			} 
			finally 
			{
				try 
				{
					if (writer != null) 
					{
						writer.close();
					}
				} 
				catch (IOException e) 
				{
					e.printStackTrace();
				}
			}
		}
		else
		{
			//FAIL
			System.out.println("File not accessible");
		}
	}
}